package com.cbh.domain.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbh.common.id.IdWorker;
import com.cbh.entity.Option;
import com.cbh.entity.Product;
import com.cbh.entity.enums.State;
import com.cbh.mapper.ProductMapper;

@Service
public class ProductService {
	private static final Log logger = LogFactory.getLog(ProductService.class);
	@Autowired
	ProductMapper proMapper;

	/**
	 * 保存产品信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String save(Product ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		ob.setProState(State.EFFECTIVE.getCode());
		if (StringUtils.isEmpty(ob.getProId())) {
			return insert(ob);
		}
		return update(ob);
	}

	/**
	 * 保存产品信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String insert(Product ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		ob.setProId(IdWorker.getId().toString());
		if (null == ob.getProName()) {
			throw new Exception("请传入名称");
		}
		if (null == ob.getProDetail()) {
			throw new Exception("请传入介绍");
		}
		if (null == ob.getProIco()) {
			throw new Exception("请传入图标");
		}
		if (null == ob.getProOptionType()) {
			throw new Exception("请传入选项类型");
		}
		dataHandle(ob);
		if (null == ob.getOptions() || ob.getOptions().isEmpty()) {
			throw new Exception("请传入选项列表");
		}

		try {
			Integer re = proMapper.insert(ob);
			if (re > 0) {
				return ob.getProId();
			} else {
				throw new Exception("保存数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("保存数据失败");
		}
	}

	private void dataHandle(Product ob) {
		if (null != ob.getProOptionType() && "check".equals(ob.getProOptionType())) {
			List<Option> options = new ArrayList<>();
			options.add(new Option().def("未选择", "0"));
			options.add(new Option().def("已选择", "1"));
			ob.setOptions(options);
		}
		if (null != ob.getHasFranchise() && "1".equals(ob.getHasFranchise()) && null == ob.getFranchises()) {
			List<Option> franchises = new ArrayList<>();
			franchises.add(new Option().def("未选择", "0"));
			franchises.add(new Option().def("已选择", "1"));
			ob.setFranchises(franchises);
		}
		if (null == ob.getProCategories()) {
			if (null != ob.getProCategoriesName()) {
				ob.setProCategoriesName(null);
			}
		} else {
			if (ob.getProCategories().equals("insurance")) {
				ob.setProCategoriesName("交强险");
			}
			if (ob.getProCategories().equals("business")) {
				ob.setProCategoriesName("商业险");
			}
		}
	}

	/**
	 * 保存产品信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String update(Product ob) throws Exception {
		try {
			dataHandle(ob);
			Integer re = proMapper.update(ob);
			if (re > 0) {
				return ob.getProId();
			} else {
				throw new Exception("更新数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("更新数据失败");
		}
	}

	/**
	 * 禁用产品
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public String invable(String id) throws Exception {
		Product ob = new Product().updateState(id, State.INVABLE.getCode());
		return update(ob);
	}

	/**
	 * 逻辑删除产品
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public String delete(String id) throws Exception {
		Product ob = new Product().updateState(id, State.DELETED.getCode());
		return update(ob);
	}

	/**
	 * 真删除产品
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public boolean realDelete(String id) throws Exception {
		Product ob = new Product().id(id);
		try {
			Integer re = proMapper.delect(ob);
			return re == 1;
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("删除数据失败");
		}
	}

	/**
	 * 根据proId获取产品信息
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public Product getOne(String id) throws Exception {
		if (null == id) {
			throw new Exception("主键为空");
		}
		Product ob = null;
		try {
			ob = proMapper.queryOne(new Product().id(id));
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return ob;
	}

	/**
	 * 获取全部有效的产品
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Product> getAll() throws Exception {
		List<Product> obs = null;
		try {
			obs = proMapper.queryAll(new Product().state(State.EFFECTIVE.getCode()));
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return obs;
	}
}
